<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of EditController
 *
 * @author Admin
 */
class User_EditController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function preDispatch() {
        $auth = Zend_Auth::getInstance();        
        if (!$auth->hasIdentity()) {
            $this->_redirect('/user/login');
        }
        
        $view = new Zend_View();
        $view->headTitle(' > Edit User');
        $this->view->userDetail = $auth->getIdentity();
    }

    public function indexAction() {
        $this->_helper->layout->disableLayout();
        
        $userModel = new Application_Model_UserModel();
        //create form
        $form = new Application_Form_users_UserForm();

        $userId = $this->_request->getParam('id');
        $userDetail = $userModel->getUser('id', $userId);

        $form->populate(array(
            'firstName' => $userDetail['fname'],
            'lastName' => $userDetail['lname'],
            'email' => $userDetail['email'],
            'cell' => $userDetail['cell'],
            'phone' => $userDetail['phone'],
            'role' => $userDetail['role'],
            'department' => $userDetail['department'],
            'status' => $userDetail['status']
        ));        
        if($userDetail['role']!='superadmin'){
            $form->department->setAttrib('disabled', false);
        }
        if($this->view->userDetail->role == 'admin'){
            $form->role->setAttrib('disabled', '');
            $form->department->setAttrib('disabled', '');
        }
        // if request is post        
        if ($this->_request->isPost()) {            
            $validator = new Zend_Validate_Db_NoRecordExists(
                            array(
                                'table' => 'user_mgmt',
                                'field' => 'email',
                                'exclude' => array(
                                    'field' => 'id',
                                    'value' => $userId
                                )
                            )
            );
          
            $form->removeElement('confirmPassword');
            $form->password->setRequired(false);
            //$form->password->removeValidator('PasswordConfirmation');
            $form->email->addValidators(array($validator));
            print_r($_POST);
            // if form is valid
            if ($form->isValid($_POST)) {

                $data = array(
                    'id' => $userId,
                    'fname' => $this->_request->getPost('firstName'),
                    'lname' => $this->_request->getPost('lastName'),
                    'email' => $this->_request->getPost('email'),
                    'cell' => $this->_request->getPost('cell'),
                    'phone' => $this->_request->getPost('phone'),
                    'role' => $this->_request->getPost('role'),
                    'department' => $this->_request->getPost('department'),
                    'status' => $this->_request->getPost('status')
                );
                $password = $this->_request->getPost('password');
                if (!empty($password)) {
                    $data['password'] = $this->_request->getPost('password');
                }
                $userModel->save($data);

             //   $this->_redirect("user/list");    
            }
        }

        // assign form variable to view
        $this->view->form = $form;
    }

}